<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 角色ajax类
 * @package  ajax/backend/system
 * @author   create by allen.qiang
 */

quanli_require('module/base/system/sysRole.class.php');

class ajax_roleList {

	public function execute() {

        if (isset($_REQUEST['operFlg']) && is_numeric($_REQUEST['operFlg']) && $_REQUEST['operFlg'] > 0) {

            switch ($_REQUEST['operFlg']) {

                case 1:
                    $this->seachRoleList();//显示列表数据
                    break;

                case 2: //角色详细查询
                    $this->searchRoleDetail();
                    break;

                case 3: //查询角色权限信息
                    $this->searchRolePermission();
                    break;

                case 100:

                    if (!isset($_REQUEST['act']) || !in_array($_REQUEST['act'], array('del', 'edit', 'permisstion'))) {
                        returnJsonData('参数错误');
                    }
                    if ($_REQUEST['act'] == 'edit') {

                        $this->edit();//新增、修改角色

                    } else if ($_REQUEST['act'] == 'del') {

                        $this->deleteRole();//删除角色

                    } else if ($_REQUEST['act'] == 'permisstion') {

                        $this->editRolePermission();//给角色分配权限

                    }

                    break;

                default:
                    returnJsonData('参数错误');
                    break;
            }

        } else {

            returnJsonData('参数错误');

        }

	}

    /**
     * 查询角色权限信息
     */
    private function searchRolePermission() {

        if (!isset($_REQUEST['id']) || !quanli_is_int($_REQUEST['id'])) {
            returnJsonData('参数错误');
        }

        //实例角色类
        $sysRole = new sysRole();

        //查询角色权限信息
        $permission_array = $sysRole->searchRolePermission( $_REQUEST['id'] );

        if ($permission_array) {
            returnJsonData('ok', 200, $permission_array);
        } else {
            returnJsonData('角色权限没有找到', 404);
        }

    }

    /**
     * 角色详细查询
     */
    private function searchRoleDetail() {

        if (!isset($_REQUEST['id']) || !quanli_is_int($_REQUEST['id'])) {
            returnJsonData('参数错误');
        }

        //实例角色类
        $sysRole = new sysRole();

        //查询角色信息
        $arg_array = array(
            'roleID' => $_REQUEST['id']
        );
        $role_array = $sysRole->searchRole( $arg_array );

        if ($role_array) {
            returnJsonData('ok', 200, $role_array[0]);
        } else {
            returnJsonData('角色组信息没有找到', 404);
        }

    }

	
	//查询角色列表信息
	private function seachRoleList() {

		global $G_PGLOB;

        //实例角色类
        $sysRole = new sysRole();
		
		//查询角色信息
        $arg_array = $_REQUEST;
		$role_array = $sysRole->searchRole( $arg_array );

        //组织返回的数据
        $result_array = array(
            'page' => $arg_array['page'],
            'total' => $arg_array['count'],
            'rows' => array()
        );
		
		if ($role_array) {

            $edit_permission = $G_PGLOB->has_permission(6);

            foreach ($role_array as &$role) {

                if ($edit_permission) {
                    $result_array['rows'][] = array(
                        'id' => $role['roleID'],
                        'cell' => array(
                            '<a href="javascript:void(0);" onclick="role.edit.open(' . $role['roleID'] . ');">修改</a>&nbsp;&nbsp;'.
                            '<a href="javascript:void(0);" onclick="role.permission.search(' . $role['roleID'] . ');">权限</a>',
                            $role['roleID'],
                            $role['roleName'],
                            $role['roleGroupName'],
                            $role['memo']
                        )
                    );
                } else {
                    $result_array['rows'][] = array(
                        'id' => $role['roleID'],
                        'cell' => array(
                            $role['roleID'],
                            $role['roleName'],
                            $role['roleGroupName'],
                            $role['memo']
                        )
                    );
                }

			}

		}

		echo json_encode($result_array);

	}

    /**
     * 新增/修改角色
     */
    private function edit() {

		global $G_PGLOB;

        //实例角色类
        $sysRole = new sysRole();
		
		//新增/修改角色
        $arg_array = $_REQUEST;
        $arg_array['operator'] = $G_PGLOB->currentUserID;
		$result_array = $sysRole->editRole( $arg_array );

        returnJsonData($result_array['message'], $result_array['code']);

	}
	
	private function editRolePermission() {

        if (!isset($_REQUEST['p_roleID']) || !quanli_is_int($_REQUEST['p_roleID'])) {
            returnJsonData('参数错误');
        }

		global $G_PGLOB;

        //实例角色类
        $sysRole = new sysRole();
		
		//调用角色分配方法
        $arg_array = $_REQUEST;
        $arg_array['roleID'] = $_REQUEST['p_roleID'];
        $arg_array['operator'] = $G_PGLOB->currentUserID;
		$result_array = $sysRole->editRolePermission( $arg_array );
		
		echo json_encode($result_array);

	}

    /**
     * 批量删除角色
     */
    private function deleteRole () {

        if (!isset($_REQUEST['r_array']) || empty($_REQUEST['r_array'])) {
            $this->toReturn('请选择要删除的角色');
        }

        //实例角色类
        $sysRole = new sysRole();

        //批量删除角色信息
		$result_array = $sysRole->deleteRole( explode(',', $_REQUEST['r_array']) );

        returnJsonData($result_array['message'], $result_array['code']);

	}

}